/*
 * SoundReader.cpp
 *
 *  Created on: 2009/02/04
 *      Author: Yongshik Cho
 */

#include "SoundReader.h"
#include "../SoundManager.h"

const std::string SoundReader::_NAME_TAG		= "name";
const std::string SoundReader::_PATH_TAG		= "path";
const std::string SoundReader::_ROLLOFF_TAG		= "Rolloff";
const std::string SoundReader::_AUDIBLE_TAG		= "Audible";
const std::string SoundReader::_VOLUME_TAG		= "Volume";
const std::string SoundReader::_LEFT_TAG		= "left";
const std::string SoundReader::_RIGHT_TAG		= "right";
const std::string SoundReader::_POSITION_TAG	= "Position";
const std::string SoundReader::_X_TAG			= "x";
const std::string SoundReader::_Y_TAG			= "y";
const std::string SoundReader::_VELOCITY_TAG	= "Velocity";
const std::string SoundReader::_DIRECTION_TAG	= "Direction";
const std::string SoundReader::_LOOPTYPE_TAG	= "LoopType";
const std::string SoundReader::_LOOPCOUNT_TAG	= "LoopCount";
const std::string SoundReader::_LOOPCOUNT_VALUE	= "LOOP_COUNT";
const std::string SoundReader::_ENDLESS_VALUE	= "ENDLESS";
const std::string SoundReader::_TRUE_TAG		= "true";
const std::string SoundReader::_FALSE_TAG		= "false";


Audible* SoundReader::parse(TiXmlElement* element)
{
	/*
	 * 	<AltoSound path='Bomb.ogg' Rolloff=0>
			<Volume left=1 right=1>
			<Position x=0 y=0/>
			<Velocity x=0 y=0/>
			<Direction x=0 y=0/>
		</AltoSound>
	 *
	 */

	double rolloff = 0.f;
	char* sAudible;

	char* path = (char*)element->Attribute(_PATH_TAG.c_str());
	element->Attribute(_ROLLOFF_TAG.c_str(), &rolloff);
	sAudible = (char*)element->Attribute(_AUDIBLE_TAG.c_str());

	TiXmlElement*node = element->FirstChildElement(_PATH_TAG.c_str()); if(node) path = (char*)node->GetText();
	node = element->FirstChildElement(_ROLLOFF_TAG.c_str()); if(node) rolloff = atof(node->GetText());
	node = element->FirstChildElement(_AUDIBLE_TAG.c_str()); if(node) sAudible = (char*)node->GetText();

	if(!path)
		return 0;

#ifdef UNICODE
	TCHAR wstr[512]; memset(wstr, 0, sizeof(wstr));
	::MultiByteToWideChar(CP_UTF8, 0, path, (int32)strlen(path), wstr, 512);
	Sound* sound = SoundManager::GetInstance().load(wstr);
#else
	Sound* sound = SoundManager::GetInstance().load(path);
#endif

	if(!sound)
		return 0;

	Audible* audible = new Audible(sound);
	if(!audible)
		AltoThrow(Exception::ERR_MEMORY);

	char* sName = (char*)element->Attribute(_NAME_TAG.c_str());
	node = element->FirstChildElement(_NAME_TAG.c_str()); if(node) sName = (char*)node->GetText();

	if(sName)
#ifdef UNICODE
	{
		TCHAR wstr[512]; memset(wstr, 0, sizeof(wstr));
		::MultiByteToWideChar(CP_UTF8, 0, sName, (int32)strlen(sName), wstr, 512);
		audible->setName(wstr);
	}
#else
		audible->setName(sName);
#endif


	if(sAudible && _TRUE_TAG == sAudible)
		audible->setAudible(true);
	else
		audible->setAudible(false);

	int32 left, right, x, y;
	TiXmlElement* volume = element->FirstChildElement(_VOLUME_TAG.c_str());
	if(volume)
	{
		volume->Attribute(_LEFT_TAG.c_str(), &left);
		node = volume->FirstChildElement(_LEFT_TAG.c_str()); if(node) left = atoi(node->GetText());
		volume->Attribute(_RIGHT_TAG.c_str(), &right);
		node = volume->FirstChildElement(_RIGHT_TAG.c_str()); if(node) right = atoi(node->GetText());
	}

	TiXmlElement* position = element->FirstChildElement(_POSITION_TAG.c_str());
	if(position)
	{
		position->Attribute(_X_TAG.c_str(), &x);
		node = position->FirstChildElement(_X_TAG.c_str()); if(node) x = atoi(node->GetText());
		position->Attribute(_Y_TAG.c_str(), &y);
		node = position->FirstChildElement(_Y_TAG.c_str()); if(node) y = atoi(node->GetText());

		audible->setPosition(Vector2D((float)x, (float)y));
	}

	TiXmlElement* velocity = element->FirstChildElement(_VELOCITY_TAG.c_str());
	if(velocity)
	{
		velocity->Attribute(_X_TAG.c_str(), &x);
		node = velocity->FirstChildElement(_X_TAG.c_str()); if(node) x = atoi(node->GetText());
		velocity->Attribute(_Y_TAG.c_str(), &y);
		node = velocity->FirstChildElement(_Y_TAG.c_str()); if(node) y = atoi(node->GetText());

		audible->setVelocity(Vector2D((float)x, (float)y));
	}

	TiXmlElement* direction = element->FirstChildElement(_DIRECTION_TAG.c_str());
	if(direction)
	{
		direction->Attribute(_X_TAG.c_str(), &x);
		node = direction->FirstChildElement(_X_TAG.c_str()); if(node) x = atoi(node->GetText());
		direction->Attribute(_Y_TAG.c_str(), &y);
		node = direction->FirstChildElement(_Y_TAG.c_str()); if(node) y = atoi(node->GetText());

		audible->setDirection(Vector2D((float)x, (float)y));
	}

	char* loopType = (char*)element->Attribute(_LOOPTYPE_TAG.c_str());
	node = element->FirstChildElement(_LOOPTYPE_TAG.c_str()); if(node) loopType = (char*)node->GetText();
	if(loopType)
	{
		if(_LOOPCOUNT_VALUE == loopType)
			audible->setLoopType(Audible::LOOP_COUNT);
		else if(_ENDLESS_VALUE == loopType)
			audible->setLoopType(Audible::ENDLESS);
	}
	else audible->setLoopType(Audible::LOOP_COUNT);

	int32 loopCount = 0;
	element->Attribute(_LOOPCOUNT_TAG.c_str(), &loopCount);
	node = element->FirstChildElement(_LOOPCOUNT_TAG.c_str()); if(node) loopCount = atoi(node->GetText());

	audible->setLoopCount(loopCount);

	return audible;
}

